///*
// *
// *  * | Licensed 未经许可不能去掉「OPENIITA」相关版权
// *  * +----------------------------------------------------------------------
// *  * | Author: xw2sy@163.com
// *  * +----------------------------------------------------------------------
// *
// *  Copyright [2024] [OPENIITA]
// *
// *  Licensed under the Apache License, Version 2.0 (the "License");
// *  you may not use this file except in compliance with the License.
// *  You may obtain a copy of the License at
// *
// *     http://www.apache.org/licenses/LICENSE-2.0
// *
// *  Unless required by applicable law or agreed to in writing, software
// *  distributed under the License is distributed on an "AS IS" BASIS,
// *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// *  See the License for the specific language governing permissions and
// *  limitations under the License.
// * /
// */
//
//package cc.iotkit.test;
//
//import cc.iotkit.common.utils.ThreadUtil;
//import cc.iotkit.plugins.tcp.parser.*;
//import cn.hutool.core.util.HexUtil;
//import cn.hutool.core.util.RandomUtil;
//import io.vertx.core.AbstractVerticle;
//import io.vertx.core.buffer.Buffer;
//import io.vertx.core.net.NetClient;
//import io.vertx.core.net.NetClientOptions;
//import io.vertx.core.net.NetSocket;
//import io.vertx.core.parsetools.RecordParser;
//import lombok.extern.slf4j.Slf4j;
//
//import java.util.concurrent.TimeUnit;
//import java.util.concurrent.atomic.AtomicInteger;
//
///**
// *
// *
// * 
// */
//@Slf4j
//public class TcpClientVerticle extends AbstractVerticle {
//
//    private NetClient netClient;
//
//    private NetSocket socket;
//
//    private byte cmd = 0x10;
//
//    private String pk = "cGCrkK7Ex4FESAwe";
//
//    private AtomicInteger atMid = new AtomicInteger(0);
//
//    @Override
//    public void start() {
//        initClient();
//    }
//
//    @Override
//    public void stop() {
//        if (null != netClient) {
//            netClient.close();
//        }
//    }
//
//    private void initClient() {
//        NetClientOptions options = new NetClientOptions();
//        options.setReconnectAttempts(Integer.MAX_VALUE);
//        options.setReconnectInterval(20000L);
//        netClient = vertx.createNetClient(options);
//        RecordParser parser = DataReader.getParser(this::handle);
//
//        netClient.connect(6883, "127.0.0.1", result -> {
//            if (result.succeeded()) {
//                log.info("connect tcp success");
//                socket = result.result();
//                socket.handler(parser);
//                //注册
//                byte[] pkBytes = pk.getBytes();
//                send(DataSendPackage.builder()
//                        .cmd(cmd)
//                        .code(DataRevPackage.CMD_QUERY_SYSTEM)
//                        .length((byte)(pk.length() + 2))
//                        .payload(pkBytes)
//                        .build());
//            } else {
//                log.error("connect tcp error", result.cause());
//            }
//        });
//    }
//
//    private void send(DataSendPackage data) {
//        Buffer buffer = DataEncoder.encode(data);
//        log.info("send data:{}", HexUtil.encodeHexStr(buffer.getBytes()));
//        socket.write(buffer);
//    }
//
//    public void handle(Buffer buffer) {
//        log.info("receive server data:{}", buffer.toString());
//        DataRevPackage data = DataDecoder.decode(buffer);
//        if (data.getCode() == DataRevPackage.CMD_QUERY_SYSTEM) {
//            int rst = Buffer.buffer(data.getPayload()).getInt(0);
//            if (rst == 0) {
//                log.info("device:{} register success", data.getCmd());
//                //定时心跳
//                ThreadUtil.newScheduled(1, "heartbeat")
//                        .scheduleWithFixedDelay(this::heartbeat, 10, 30, TimeUnit.SECONDS);
//                //随机上报数据
//                ThreadUtil.newScheduled(1, "reportData")
//                        .scheduleWithFixedDelay(this::reportData, 20, 1, TimeUnit.SECONDS);
//            }
//        }
//    }
//
//    private void heartbeat() {
//
//    }
//
//    private void reportData() {
//        if (RandomUtil.randomInt() % 3 == 0) {
//            //随机
//            return;
//        }
//    }
//
//}